//
//  SDDetectionViewController.m
//  skinDetection
//
//  Created by  liangzhun on 2018/10/29.
//  Copyright © 2018年  liangzhun. All rights reserved.
//

#import "SDDetectionViewController.h"
#import "SDDetectionCell.h"
#import "SDDetectionFooter.h"
#import "SDDetectionDataModel.h"
#import "SDDiagnoseSkinVC.h"


static NSString *const cellIden = @"SDDetectionCell";
static NSString *const headerIden = @"SDCompareDetectionHeaderView";
@interface SDDetectionViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong) SDDetectionFooter *footerView;

@end

@implementation SDDetectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _navHeight.constant = SDNavBar_Height;
    self.tableView.estimatedRowHeight = 310;
    _tableView.rowHeight = UITableViewAutomaticDimension;
    [self.tableView registerNib:[UINib nibWithNibName:cellIden bundle:nil] forCellReuseIdentifier:cellIden];
   
    [self.footerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom).offset(-(10 + SDBottom_Height));
        make.height.mas_equalTo(65);
    }];
    
    [self userIsOrNotDetection];
    
    //kvo
    [[SDDetectionDataModel shareInstance] addObserver:self forKeyPath:@"isDetectionArray" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
    [[SDDetectionDataModel shareInstance] addObserver:self forKeyPath:@"type" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
    
}
-(void)dealloc
{
    [[SDDetectionDataModel shareInstance] removeObserver:self forKeyPath:@"type"];
    [[SDDetectionDataModel shareInstance] removeObserver:self forKeyPath:@"isDetectionArray"];
    
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    [self userIsOrNotDetection];
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    SDDetectionCell*cell = [tableView dequeueReusableCellWithIdentifier:cellIden];
    cell.contentLab.text = [SDDetectionDataModel shareInstance].type == 1? light_line_Explain_B:[SDDetectionDataModel shareInstance].type == 5? light_line_Explain_UV:light_line_Explain_P;
    
    NSString *kindLight = [SDDetectionDataModel shareInstance].type == 1? light_line_B:[SDDetectionDataModel shareInstance].type == 5? light_line_UV:light_line_P;
    NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:kindLight attributes:@{NSForegroundColorAttributeName:SDWhiteColor}];
    [att addAttribute:NSForegroundColorAttributeName value:RGBA(250, 207, 92, 1) range:NSMakeRange(4, 2)];
    [cell.titleLab setAttributedText:att];
   
    return cell;
    
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}


-(SDDetectionFooter *)footerView
{
    if (!_footerView) {
        
        _footerView = [SDMethodTool getViewWithViewName:NSStringFromClass([SDDetectionFooter class])];
        [_footerView setTypeBtn];
        
        for (int b = 0; b < _footerView.skinBtnArray.count; b++) {
            
            UIImageView *imageSelect = _footerView.selectImageArray[b];
            imageSelect.hidden = ![[SDDetectionDataModel shareInstance].isDetectionArray[b] boolValue];
            UIButton *typeBtn = _footerView.skinBtnArray[b];
            [typeBtn addTarget:self action:@selector(selectType:) forControlEvents:UIControlEventTouchUpInside];
        }
        
       
        [self.view addSubview:_footerView];
        
    }
    
    return _footerView;
    
}
//判断皮肤是否已拍照
- (void)userIsOrNotDetection
{
    
    [[SDDetectionDataModel shareInstance].isDetectionArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSNumber *number = obj;
        UIImageView *imageView = self.footerView.selectImageArray[idx];
        imageView.hidden = ![number boolValue];
    }];
    
     _footerView.selectBtn = _footerView.skinBtnArray[[SDDetectionDataModel shareInstance].type - 1];
    
}
- (void)selectType:(UIButton *)sender
{
    
    [SDDetectionDataModel shareInstance].type = sender.tag;
    self.footerView.selectBtn = sender;
    [self.tableView reloadData];
    [self goDiagnoseSkinVC];

}
//进入皮肤诊断页面
- (void)goDiagnoseSkinVC
{
    NSArray *image = @[@"2018-11-20",@"4491540889882_.pic",@"4511540889883_.pic",@"4521540889884_.pic_hd",@"4531540889885_.pic_hd"];
    SDDetectionDataModel *data = [SDDetectionDataModel shareInstance];
    
    NSString *imageStr = ([self getGGMGMKImage].length > 0)&&(data.type > 1 && data.type <5)? [self getGGMGMKImage]:image[data.type - 1];
        
    [data.detectionImageDic setObject:imageStr forKey:[NSString stringWithFormat:@"%lu",(unsigned long)data.type]];
    SDDiagnoseSkinVC *vc = [SDMethodTool getViewControlWithIdentifier:NSStringFromClass([SDDiagnoseSkinVC class])];
    [self.navigationController pushViewController:vc animated:YES];
    
}
//获取色素 敏感度 毛孔对应的拍照
- (NSString *)getGGMGMKImage
{
    for (int i = 2; i < 5; i++) {
        NSString *image = [[SDDetectionDataModel shareInstance].detectionImageDic objectForKey:[NSString stringWithFormat:@"%d",i]];
        if (image.length > 0)return image;
    }
    
    return @"";
    
}
- (IBAction)back:(UIButton *)sender {
    [self.navigationController popToRootViewControllerAnimated:YES];
}
@end
